var server = require('../../../utils/server');
var cPage = 0;
var ctype = "";
Page({
    tabClick: function (e) {
        var index = e.currentTarget.dataset.index
        var types = ["", "0", "5", "40", "50"]


        var classs = ["text-normal", "text-normal", "text-normal", "text-normal", "text-normal", "text-normal"]
        classs[index] = "text-select"
        this.setData({ tabClasss: classs, tab: index })
        cPage = 0;
        ctype = types[index];
        this.data.orders = [];
        this.getOrderLists(types[index], cPage);
    
  },
    pay: function (e) {
      var index = e.currentTarget.dataset.index;
      let that = this
        server.send('/common/change-endtime-user', 'POST', {
            shop_id: index
        }).then(res => {
          if (res.list.tenant.status==0){
              wx.showToast({
                title: '暂时不能购买！',
                icon: "none"
              })
          } else if (res.list.tenant.status == 1){
            wx.setStorageSync('shop_id', index)
            wx.navigateTo({
              url: '../payment/payment'
            })
          }
        })
    },
    delete: function (e) {
        let index = e.currentTarget.dataset.index;
        // console.log(index)
        let that = this
        wx.showModal({
            title: '提示',
            showCancel: true,
            content: '确定删除订单吗？',
            success: function (res) {
                if (res.confirm) {
                    let _params = {
                        username: getApp().globalData.username,
                        token: getApp().globalData.token,
                        order_id: index
                    }
                    // server.postJSON('/ordermanage/del',_params,(res)=>{
                    //   wx.showToast({
                    //     title: res.data.msg,
                    //     icon: 'success',
                    //     duration: 2000
                    //   });
                    //   that.getOrderLists(ctype);
                    // })
                    server.send('/ordermanage/del', 'POST', _params).then(res => {
                        wx.showToast({
                            title: res.msg,
                            icon: 'success',
                            duration: 2000
                        });
                        that.getOrderLists(ctype);
                    })
                }
            }
        })
    },
    cancel: function (e) {
        let index = e.currentTarget.dataset.index;
        // console.log(index)
        let that = this;
        wx.showModal({
            title: '提示',
            showCancel: true,
            content: '确定取消订单吗？',
            success: function (res) {
                if (res.confirm) {
                    let _params = {
                        username: getApp().globalData.username,
                        token: getApp().globalData.token,
                        order_id: index
                    }
                    //   server.postJSON('/ordermanage/orderdel',_params,function(res){
                    //     wx.showToast({
                    //         title: res.data.msg,
                    //         icon: 'success',
                    //         duration: 2000 
                    //       })
                    //     cPage = 0;
                    //     that.data.orders = [];
                    //     that.getOrderLists(ctype);
                    //   });
                    server.send('/ordermanage/orderdel', 'POST', _params).then(res => {
                        wx.showToast({
                            title: res.msg,
                            icon: 'success',
                            duration: 2000
                        })
                        cPage = 0;
                        that.data.orders = [];
                        that.getOrderLists(ctype);
                    });
                }
            }
        })
    },

    confirm: function (e) {
        var index = e.currentTarget.dataset.index;
        var order = this.data.orders[index];
        var that = this;
        wx.showModal({
            title: '提示',
            showCancel: true,
            content: '确定已收货吗？',
            success: function (res) {

                if (res.confirm) {


                    var user_id = getApp().globalData.userInfo.user_id

                    server.getJSON('/User/orderConfirm/user_id/' + user_id + "/order_id/" + order['order_id'], function (res) {
                        wx.showToast({ title: res.data.msg, icon: 'success', duration: 2000 })
                        cPage = 0;

                        that.data.orders = [];
                        that.getOrderLists(ctype, 0);
                    });

                }
            }
        })



    },



    details: function (e) {
        var index = e.currentTarget.dataset.index;
        var goods = this.data.orders[index];
        wx.navigateTo({
            url: '../details/index?order_id=' + goods['order_id']
        });
    },
    onReachBottom: function () {
        this.getOrderLists(ctype, ++cPage);
        wx.showToast({
            title: '加载中',
            icon: 'loading'
        })
    },
    onPullDownRefresh: function () {
        cPage = 0;
        this.data.orders = [];
        this.getOrderLists(ctype, 0);
    },
    data: {
        orders: [],
        tabClasss: ["text-select", "text-normal", "text-normal", "text-normal", "text-normal"],
        address:'',
        status:'',
    },
    getOrderLists: function (ctype) {
        var that = this;
        let _params = {
            username: getApp().globalData.username,
            token: getApp().globalData.token,
            order_status: ctype
        }
        // server.postJSON('/person/orderinfo', _params, function (res) {
        //     var orders = res.data.list;
        //     // console.log(res)
        //     that.setData({
        //         orders: orders
        //     });
        // });
        server.send('/person/orderinfo', 'POST', _params).then(res => {
            var orders = res.list;
            // console.log(res)
            that.setData({
                orders: orders
            });
        });
    },
    onShow: function () {
        cPage = 0;
        this.data.orders = [];
        this.getOrderLists(ctype);

    },
    onLoad: function (option) {
        // 页面显示
        if (option.cid == "WAITSEND") {
            ctype = '5';
            var tabClasss = ["text-normal", "text-normal", "text-select", "text-normal", "text-normal"];
            this.setData({ tabClasss: tabClasss });
        }
        if (option.cid == "FINISH") {
            ctype = '50';
            var tabClasss = ["text-normal", "text-normal", "text-normal", "text-normal", "text-select"];
            this.setData({ tabClasss: tabClasss });
        }
        if (option.cid == "NO") {
            ctype = '';
            var tabClasss = ["text-select", "text-normal", "text-normal", "text-normal", "text-normal"];
            this.setData({ tabClasss: tabClasss });
        }
        cPage = 0;

        this.data.orders = [];
    }
});